package com.hsnn.medstgmini.base.std.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hsnn.medstgmini.base.dict.dao.DicPharDao;
import com.hsnn.medstgmini.base.dict.model.DicPhar;
import com.hsnn.medstgmini.base.dict.model.DicSys;
import com.hsnn.medstgmini.base.std.dao.StdDrugInfoDao;
import com.hsnn.medstgmini.base.std.model.StdDrugInfo;
import com.hsnn.medstgmini.base.std.model.StdDruginfoDetail;
import com.hsnn.medstgmini.base.std.model.StdDruginfoDetailLog;
import com.hsnn.medstgmini.base.std.model.StdDruginfoLog;
import com.hsnn.medstgmini.base.std.model.form.StdDrugInfoForm;
import com.hsnn.medstgmini.base.std.service.StdDrugInfoManager;
import com.hsnn.medstgmini.model.IsStatus;
import com.hsnn.medstgmini.model.MegerStatus;
import com.hsnn.medstgmini.model.Status;
import com.hsnn.medstgmini.util.ConstantsUtil;
import com.hsnn.medstgmini.util.FormatDate;
import com.hsnn.medstgmini.util.Pagination;

@Service("stdDrugInfoManager")
public class StdDrugInfoManagerImpl implements StdDrugInfoManager {
	@Autowired
	private StdDrugInfoDao stdDrugInfoDao;
	@Autowired
	private DicPharDao dicPharDao;
	
	private final String DRUG_ATTR = "110";
	private final String PATTERN_TYPE = "111";
	private final String DRUG_SOURCE = "112";
	
	@Override
	public Pagination getAllByPage(Pagination page) {
		page = stdDrugInfoDao.getAllByPage(page);
		page.setRows(this.formatData((List<StdDrugInfo>)page.getRows()));
		return page;
	}
	
	@Transactional
	public String addData(StdDrugInfoForm form) {
		// TODO Auto-generated method stub
		return stdDrugInfoDao.addData(form);
	}
	
	private String pharstr = "";
	/**
	 * 
	 *@category 递归药理父级，返回以逗号分割的字符串
	 *@author qiulei
	 *@date 2015年6月10日 上午8:20:06
	 *@param data
	 *@param rePhar
	 *@return
	 *@return String
	 */
	private void getPharStr(DicPhar data){
		if (null != data.getFatherId() && !"".equals(data.getFatherId()) && !"0".equals(data.getFatherId())){
			getPharStr(dicPharDao.getDicPharById(data.getFatherId()));
			pharstr += data.getPharId()+",";
		} else {
			pharstr += data.getPharId()+",";
		}
	}
	
	public StdDrugInfoForm getStdInfo(String id){
		StdDrugInfo druginfo = stdDrugInfoDao.getDrugInfo(id);
		StdDruginfoDetail druginfodetail = stdDrugInfoDao.getDrugInfoDetail(id);
		StdDrugInfoForm form = new StdDrugInfoForm();
		form.setDrugId(druginfo.getDrugId());
		form.setDrugCode(druginfo.getDrugCode());
		form.setDrugName(druginfo.getDrugName());
		form.setDrugNameEng(druginfo.getDrugNameEng());
		form.setBusName(druginfo.getBusName());
		
		if (null != druginfo.getDrugform()){
			form.setFormName(druginfo.getDrugform().getFormName());
			if (null != druginfo.getDrugform().getFormId() && !"".equals(druginfo.getDrugform().getFormId())){
				form.setFormId(druginfo.getDrugform().getFormId());
				if (null != druginfo.getDrugform().getFatherCode()){
					form.setDrugform1(druginfo.getDrugform().getFatherCode().getFormId());
					form.setDrugform2(form.getFormId());
				} else {
					form.setDrugform1(form.getFormId());
				}
			}
		}
		if (null != druginfo.getDrugspec()){
			if (null != druginfo.getDrugspec().getSpecId() && !"".equals(druginfo.getDrugspec().getSpecId().trim())){
				form.setSpecId(druginfo.getDrugspec().getSpecId());
				form.setSpecName(druginfo.getDrugspec().getSpecName());
			}
		}
		
		form.setFactor(druginfo.getFactor());
		form.setUnit(druginfo.getUnit());
		form.setMaterialId(druginfo.getDrugmaterial()==null?null:druginfo.getDrugmaterial().getMaterialId());
		form.setMaterialName(druginfo.getDrugmaterial()==null?null:druginfo.getDrugmaterial().getMaterialName());
		form.setProdPompId(druginfo.getCompinfo()==null?null:druginfo.getCompinfo().getCompId());
		form.setProdPompName(druginfo.getCompinfo()==null?null:druginfo.getCompinfo().getCompName());
		form.setApprovalCode(druginfo.getApprovalCode());
		form.setApprovalDate(druginfo.getApprovalDate()==null?null:FormatDate.toShortFormat(druginfo.getApprovalDate()));
		form.setStatus(Status.getName(druginfo.getStatus()));
		form.setUpdDatetime(druginfo.getUpdDatetime()==null?"":FormatDate.toShortFormat(druginfo.getUpdDatetime()));
		form.setUpdUser(druginfo.getUpdUser());
		form.setHistoryDrugCode(druginfo.getHistoryDrugCode());
		form.setMergeStatus(druginfo.getMergeStatus()==null?"":druginfo.getMergeStatus().toString());
		form.setDrugAttr(druginfodetail.getDrugAttr()==null?"":druginfodetail.getDrugAttr().toString());
		form.setDrugAttrText(druginfodetail.getDrugAttr()==null?"":stdDrugInfoDao.getDicSys(DRUG_ATTR, druginfodetail.getDrugAttr().toString()).getDicName());
		form.setDrugIndex(druginfodetail.getDrugIndex());
		if (null != druginfodetail.getPhar()){
			form.setPharId(druginfodetail.getPhar().getPharId());
			form.setPharName(druginfodetail.getPhar().getPharName());
			pharstr = "";
			getPharStr(druginfodetail.getPhar());
			String[] phars = pharstr.split(",");
			if (null != phars && phars.length > 0){
				if (phars.length >=1 && null != phars[0] && !"".equals(phars[0].trim())){
					form.setPharbig(phars[0].trim());
				}
				if (phars.length >=2 && null != phars[1] && !"".equals(phars[1].trim())){
					form.setPhar1(phars[1].trim());
				}
				if (phars.length >=3 && null != phars[2] && !"".equals(phars[2].trim())){
					form.setPhar2(phars[2].trim());
				}
				if (phars.length >=4 && null != phars[3] && !"".equals(phars[3].trim())){
					form.setPhar3(phars[3].trim());
				}
			}
		}
		
		form.setPatternType(druginfodetail.getPatternType()==null?"":druginfodetail.getPatternType().toString());
		form.setPatternTypeText(druginfodetail.getPatternType()==null?"":stdDrugInfoDao.getDicSys(PATTERN_TYPE, druginfodetail.getPatternType().toString()).getDicName());
		form.setDrugSource(druginfodetail.getDrugSource()==null?"":druginfodetail.getDrugSource().toString());
		form.setDrugSourceText(druginfodetail.getDrugSource()==null?"":stdDrugInfoDao.getDicSys(DRUG_SOURCE, druginfodetail.getDrugSource().toString()).getDicName());
		form.setIsLowprice(druginfodetail.getIsLowprice()==null?"":druginfodetail.getIsLowprice().toString());
		form.setIsAgriculture(druginfodetail.getIsAgriculture()==null?"":druginfodetail.getIsAgriculture().toString());
		form.setIsHealthcare(druginfodetail.getIsHealthcare()==null?"":druginfodetail.getIsHealthcare().toString());
		
		form.setIsLowpriceText(druginfodetail.getIsLowprice()==null?"":IsStatus.getName(druginfodetail.getIsLowprice().toString()));
		form.setIsAgricultureText(druginfodetail.getIsAgriculture()==null?"":IsStatus.getName(druginfodetail.getIsAgriculture().toString()));
		form.setIsHealthcareText(druginfodetail.getIsHealthcare()==null?"":IsStatus.getName(druginfodetail.getIsHealthcare().toString()));
		form.setIsLowpriceText(ConstantsUtil.literStatus(druginfodetail.getIsLowprice()==null?"":druginfodetail.getIsLowprice().toString()));
		form.setIsAgricultureText(ConstantsUtil.literStatus(druginfodetail.getIsAgriculture()==null?"":druginfodetail.getIsAgriculture().toString()));
		form.setIsHealthcareText(ConstantsUtil.literStatus(druginfodetail.getIsHealthcare()==null?"":druginfodetail.getIsHealthcare().toString()));
		form.setDrugStdCode(druginfodetail.getDrugStdCode());;
		form.setDrugStdcdRemark(druginfodetail.getDrugStdcdRemark());
		form.setRemark(druginfodetail.getDrugStdcdRemark());
		return form;
	}
	private List<StdDrugInfoForm> formatData(List<StdDrugInfo> list){
    	if (null != list && list.size() >0){
    		List<StdDrugInfoForm> flist = new ArrayList<StdDrugInfoForm>();
    		for (int i = 0; i < list.size(); i ++){
    			flist.add(formartForm(list.get(i)));
    		}
    		return flist;
    	}
    	return null;
    }
	private StdDrugInfoForm formartForm(StdDrugInfo data){
		if (null == data){
			return null;
		}
		StdDrugInfoForm form = new StdDrugInfoForm();
		form.setApprovalCode(data.getApprovalCode());
		form.setApprovalDate(FormatDate.toShortFormat(data.getApprovalDate()));
		form.setBusName(data.getBusName());
		form.setDrugCode(data.getDrugCode());
		form.setDrugId(data.getDrugId());
		form.setDrugName(data.getDrugName());
		form.setDrugNameEng(data.getDrugNameEng());
		form.setDrugNameSpel(data.getDrugNameSpel());
		form.setFactor(data.getFactor());
		form.setFormId(data.getDrugform()==null?"":data.getDrugform().getFormName());
		form.setFormName(data.getDrugform()==null?"":data.getDrugform().getFormName());
		form.setHistoryDrugCode(data.getHistoryDrugCode());
		form.setMaterialName(data.getDrugmaterial()==null?"":data.getDrugmaterial().getMaterialName());
		form.setMergeStatus(data.getMergeStatus()==null?"":data.getMergeStatus().toString());
		form.setProdPompName(data.getCompinfo()==null?"":data.getCompinfo().getCompName());
		form.setSpecId(data.getDrugspec()==null?"":data.getDrugspec().getSpecName());
		form.setSpecName(data.getDrugspec()==null?"":data.getDrugspec().getSpecName());
		form.setStatus(data.getStatus()==null?"":ConstantsUtil.stdStatus(data.getStatus().toString()));
		form.setUnit(data.getUnit());
		form.setUpdDatetime(data.getUpdDatetime()==null?"":FormatDate.toShortFormat(data.getUpdDatetime()));
		form.setUpdUser(data.getUpdUser());
		return form;
	}
	@Transactional
	public String editInfo(StdDrugInfoForm form){
		return stdDrugInfoDao.editInfo(form);
	}
	/**
	 * 日志详情
	 *@category
	 *@author qiulei
	 *@date 2015年6月9日 下午1:24:39
	 *@param logid
	 *@return
	 */
	public StdDrugInfoForm drugInfoLogDetail(String logid){
		StdDruginfoLog data = stdDrugInfoDao.getLogInfo(logid);
		StdDruginfoDetailLog dataDetail = stdDrugInfoDao.getLogInfoDetail(logid);
		return formatForm(data, dataDetail);
	}
	/**
	 * 
	 *@category 日志上一次修改详情
	 *@author qiulei
	 *@date 2015年6月9日 下午2:03:16
	 *@param logid
	 *@param date
	 *@return
	 *@return StdDruginfoLog
	 */
	public StdDrugInfoForm getPreviousLog(String drugId,String date){
		StdDruginfoLog data = stdDrugInfoDao.getPreviousLog(drugId, date);
		StdDruginfoDetailLog dataDetail = new StdDruginfoDetailLog();
		if (null != data && null != data.getLogId() && !"".equals(data.getLogId())){
			dataDetail = stdDrugInfoDao.getLogInfoDetail(data.getLogId());
		}
		
		return formatForm(data, dataDetail);
	}
	
	private StdDrugInfoForm formatForm(StdDruginfoLog data,StdDruginfoDetailLog dataDetail){
		StdDrugInfoForm form = new StdDrugInfoForm();
		BeanUtils.copyProperties(data, form);
		form.setFormName(data.getDrugform()==null?"":data.getDrugform().getFormName());
		form.setSpecName(data.getDrugspec()==null?"":data.getDrugspec().getSpecName());
		form.setProdPompName(data.getCompinfo()==null?"":data.getCompinfo().getCompName());
		form.setMaterialName(data.getDrugmaterial()==null?"":data.getDrugmaterial().getMaterialName());
		form.setApprovalDate(data.getApprovalDate()==null?null:FormatDate.toShortFormat(data.getApprovalDate()));
		form.setStatus(data.getStatus()==null?"":Status.getName(data.getStatus()));
		form.setUpdDatetime(data.getUpdDatetime()==null?null:FormatDate.toFullFormat(data.getUpdDatetime()));
		form.setUpdUser(data.getSysUser()==null?null:data.getSysUser().getName());
		form.setMergeStatus(data.getMergeStatus()==null?"":MegerStatus.getName(data.getMergeStatus()));
		if (null != dataDetail) {
			BeanUtils.copyProperties(dataDetail, form);
			form.setIsLowpriceText(dataDetail.getIsLowprice()==null?"":IsStatus.getName(dataDetail.getIsLowprice().toString()));
			form.setIsAgricultureText(dataDetail.getIsAgriculture()==null?"":IsStatus.getName(dataDetail.getIsAgriculture().toString()));
			form.setIsHealthcareText(dataDetail.getIsHealthcare()==null?"":IsStatus.getName(dataDetail.getIsHealthcare().toString()));
			form.setIsLowpriceText(dataDetail.getIsLowprice()==null?"":ConstantsUtil.literStatus(dataDetail.getIsLowprice().toString()));
			form.setIsAgricultureText(dataDetail.getIsAgriculture()==null?"":ConstantsUtil.literStatus(dataDetail.getIsAgriculture().toString()));
			form.setIsHealthcareText(dataDetail.getIsHealthcare()==null?"":ConstantsUtil.literStatus(dataDetail.getIsHealthcare().toString()));
			if (null != dataDetail.getDrugAttr()){
				form.setDrugAttr(dataDetail.getDrugAttr().toString());
				DicSys dicSys = stdDrugInfoDao.getDicSys(DRUG_ATTR, dataDetail.getDrugAttr().toString());
				form.setDrugAttrText(dicSys==null?"":dicSys.getDicName());
			}
			if (null != dataDetail.getPatternType()){
				form.setPatternType(dataDetail.getPatternType().toString());
				DicSys dicSys1 = stdDrugInfoDao.getDicSys(PATTERN_TYPE, dataDetail.getPatternType().toString());
				form.setPatternTypeText(dicSys1==null?"":dicSys1.getDicName());
			}
			if (null != dataDetail.getDrugSource()){
				form.setDrugSource(dataDetail.getDrugSource().toString());
				DicSys dicSys2 = stdDrugInfoDao.getDicSys(DRUG_SOURCE, dataDetail.getDrugSource().toString());
				form.setDrugSourceText(dicSys2==null?"":dicSys2.getDicName());
			}
			
			DicPhar phar = dicPharDao.getDicPharById(dataDetail.getPharId());
			form.setPharName(phar==null?"":phar.getPharName());
		}
		
//		form.setDrugAttr(drugAttr);
		
		return form;
		
	}
	/**
	 * 启用停用
	 */
	@Transactional
	public String startOrStop(String drugId, Short status,String userId){
		return stdDrugInfoDao.startOrStop(drugId, status,userId);
	}
	@Transactional
	public String batchStartOrStop(List<StdDrugInfo> list){
		return stdDrugInfoDao.batchStartOrStop(list);
	}
	public Pagination getDrugInfoLogList(Pagination page){
		page = stdDrugInfoDao.getDrugInfoLogList(page);
		page.setRows(this.fromartLoglist((List<StdDruginfoLog>)page.getRows()));
		return page;
	}
	private List<StdDrugInfoForm> fromartLoglist(List<StdDruginfoLog> list){
		if (null != list && list.size() > 0){
			List<StdDrugInfoForm> flist = new ArrayList<StdDrugInfoForm>();
			for (int i = 0; i < list.size(); i++){
				StdDrugInfoForm form = formatForm(list.get(i), null);
				flist.add(form);
			}
			return flist;
		}
		return null;
	}
	/**
	 * 
	 *@category 获取被合并信息
	 *@author qiulei
	 *@date 2015年6月9日 下午2:51:15
	 *@param drugId
	 *@return
	 */
	public StdDrugInfoForm getMergeDrugInfo(String drugId){
		StdDrugInfo data = stdDrugInfoDao.getMergeDrugInfo(drugId);
		return formartForm(data);
	}
	/**
	 * 
	 *@category 获取状态启用，并且排除被合并的列表
	 *@author qiulei
	 *@date 2015年6月9日 下午3:17:15
	 *@param page
	 *@return
	 */
	public Pagination getValidDrugInfoList (Pagination page){
		page = stdDrugInfoDao.getValidDrugInfoList(page);
		page.setRows(this.formatData((List<StdDrugInfo>)page.getRows()));
		return page;
	}
	@Transactional
	public String drugInfoMerge(String drugOId,String drugId){
		return stdDrugInfoDao.drugInfoMerge(drugOId, drugId);
	}
}
